package com.wu.common.module.ui.widget;

/**
 * 作者:吴奎庆
 * <p>
 * 时间:2020/7/16
 * <p>
 * 用途:
 */



import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.Path.Direction;
import android.graphics.Shader.TileMode;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.security.MessageDigest;

public class GlideRoundedCornersTransform extends CenterCrop {
    private float mRadius;
    private GlideRoundedCornersTransform.CornerType mCornerType;
    private static final int VERSION = 1;
    private static final String ID = "com.bumptech.glideGlideRoundedCornersTransform.1";
    private static final byte[] ID_BYTES;

    public GlideRoundedCornersTransform(float radius, GlideRoundedCornersTransform.CornerType cornerType) {
        this.mRadius = (float) DensityUtil.dp2px(radius);
        this.mCornerType = cornerType;
    }

    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
        return this.roundCrop(pool, transform);
    }

    private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        } else {
            int width = source.getWidth();
            int height = source.getHeight();
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, TileMode.CLAMP, TileMode.CLAMP));
            paint.setAntiAlias(true);
            Path path = new Path();
            this.drawRoundRect(canvas, paint, path, width, height);
            return result;
        }
    }

    private void drawRoundRect(Canvas canvas, Paint paint, Path path, int width, int height) {
        float[] rids;
        switch(this.mCornerType) {
            case ALL:
                rids = new float[]{this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP_LEFT:
                rids = new float[]{this.mRadius, this.mRadius, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP_RIGHT:
                rids = new float[]{0.0F, 0.0F, this.mRadius, this.mRadius, 0.0F, 0.0F, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case BOTTOM_RIGHT:
                rids = new float[]{0.0F, 0.0F, 0.0F, 0.0F, this.mRadius, this.mRadius, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case BOTTOM_LEFT:
                rids = new float[]{0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, this.mRadius, this.mRadius};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP:
                rids = new float[]{this.mRadius, this.mRadius, this.mRadius, this.mRadius, 0.0F, 0.0F, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case BOTTOM:
                rids = new float[]{0.0F, 0.0F, 0.0F, 0.0F, this.mRadius, this.mRadius, this.mRadius, this.mRadius};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case LEFT:
                rids = new float[]{this.mRadius, this.mRadius, 0.0F, 0.0F, 0.0F, 0.0F, this.mRadius, this.mRadius};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case RIGHT:
                rids = new float[]{0.0F, 0.0F, this.mRadius, this.mRadius, this.mRadius, this.mRadius, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP_LEFT_BOTTOM_RIGHT:
                rids = new float[]{this.mRadius, this.mRadius, 0.0F, 0.0F, this.mRadius, this.mRadius, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP_RIGHT_BOTTOM_LEFT:
                rids = new float[]{0.0F, 0.0F, this.mRadius, this.mRadius, 0.0F, 0.0F, this.mRadius, this.mRadius};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT:
                rids = new float[]{this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius, 0.0F, 0.0F};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            case TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT:
                rids = new float[]{0.0F, 0.0F, this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius, this.mRadius};
                this.drawPath(rids, canvas, paint, path, width, height);
                break;
            default:
                throw new RuntimeException("RoundedCorners type not belong to CornerType");
        }

    }

    private void drawPath(float[] rids, Canvas canvas, Paint paint, Path path, int width, int height) {
        path.addRoundRect(new RectF(0.0F, 0.0F, (float)width, (float)height), rids, Direction.CW);
        canvas.drawPath(path, paint);
    }

    public boolean equals(Object o) {
        return o instanceof GlideRoundedCornersTransform;
    }

    public int hashCode() {
        return "com.bumptech.glideGlideRoundedCornersTransform.1".hashCode();
    }

    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);
    }

    static {
        ID_BYTES = "com.bumptech.glideGlideRoundedCornersTransform.1".getBytes(CHARSET);
    }

    public static enum CornerType {
        ALL,
        TOP_LEFT,
        TOP_RIGHT,
        BOTTOM_LEFT,
        BOTTOM_RIGHT,
        TOP,
        BOTTOM,
        LEFT,
        RIGHT,
        TOP_LEFT_BOTTOM_RIGHT,
        TOP_RIGHT_BOTTOM_LEFT,
        TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT,
        TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT;

        private CornerType() {
        }
    }
}
